package java_thinking.unit_11._11_13;

import java.util.*;

/**
 * @Description : 两种IterableClass的iterator适配方法
 * @author: cww
 * @DateTime: 2019-03-19 22:43
 */

public class MultiIterableClass extends IterableClass {
    public Iterable<String> reversed(){
        return new Iterable<String>() {
            @Override
            public Iterator<String> iterator() {
                return new Iterator<String>() {
                    int i = words.length-1;
                    @Override
                    public boolean hasNext() {
                        return i>-1;
                    }

                    @Override
                    public String next() {
                        return words[i--];
                    }
                };
            }
        };
    }

    public Iterable<String> randomized(){
        return new Iterable<String>() {
            @Override
            public Iterator<String> iterator() {
                List<String> shuffled = new ArrayList<>(Arrays.asList(words));
                //返回打乱list中的iterator
                Collections.shuffle(shuffled,new Random(47));
                return shuffled.iterator();
            }
        };
    }

    public static void main(String[] args) {
        MultiIterableClass mic = new MultiIterableClass();
        for (String s:mic){
            System.out.print(s+" ");
        }
        System.out.println();
        for (String s:mic.reversed()){
            System.out.print(s+" ");
        }
        System.out.println();
        for (String s:mic.randomized()){
            System.out.print(s+" ");
        }

    }
}
